package com.lam.mall.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.utils.StringUtils;
import com.lam.mall.entity.UserProdBrowse;
import com.lam.mall.mapper.UserProdBrowseMapper;
import com.lam.mall.param.UserProdBrowseParam;
import com.lam.mall.service.UserProdBrowseService;

/**
 * 会员商品足迹Service业务层处理
 * 
 * @author admin
 * @date 2023-05-12
 */
@Service
public class UserProdBrowseServiceImpl  implements UserProdBrowseService {

    @Autowired
    private UserProdBrowseMapper userProdBrowseMapper;

    /**
     * 查询会员商品足迹
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public UserProdBrowse selectById(String id) {

        return userProdBrowseMapper.selectById(id);
    }

    /**
     * 查询会员商品足迹列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<UserProdBrowse> selectList(UserProdBrowseParam param) {
    
    	QueryWrapper<UserProdBrowse> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq(StringUtils.isNotBlank(param.getShopId()), "shop_id", param.getShopId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getUserId()), "user_id", param.getUserId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getProdId()), "prod_id", param.getProdId());
        queryWrapper.ge(Objects.nonNull(param.getCreateTimeBegin()), "create_time", param.getCreateTimeBegin());
        queryWrapper.le(Objects.nonNull(param.getCreateTimeEnd()), "create_time", param.getCreateTimeEnd());

        return userProdBrowseMapper.selectList(queryWrapper);
    }
    
    @Override
	public Long selectCount(String userId) {
    	
    	QueryWrapper<UserProdBrowse> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("user_id", userId);
    	return userProdBrowseMapper.selectCount(queryWrapper);
	}
    
    /**
     * 新增会员商品足迹
     * 
     * @param userProdBrowse 
     * @return 
     */
    @Override
    @Transactional
    public int insert(String shopId, String prodId, String userId) {
    	
    	QueryWrapper<UserProdBrowse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("prod_id", prodId);
        UserProdBrowse br = userProdBrowseMapper.selectOne(queryWrapper);
        UserProdBrowse userProdBrowse = new UserProdBrowse();
        userProdBrowse.setCreateTime(new Date());
        if(br == null) {
        	userProdBrowse.setShopId(shopId);
        	userProdBrowse.setProdId(prodId);
        	userProdBrowse.setUserId(userId);
        	return userProdBrowseMapper.insert(userProdBrowse);
        }
        userProdBrowse.setId(br.getId());
        return userProdBrowseMapper.updateById(userProdBrowse);
    }

    /**
     * 新增会员商品足迹
     * 
     * @param userProdBrowse 
     * @return 
     */
    @Override
    @Transactional
    public int insert(UserProdBrowse userProdBrowse) {
    
        return userProdBrowseMapper.insert(userProdBrowse);
    }

    /**
     * 修改会员商品足迹
     * 
     * @param userProdBrowse 
     * @return 
     */
    @Override
    @Transactional
    public int update(UserProdBrowse userProdBrowse) {
    
        return userProdBrowseMapper.updateById(userProdBrowse);
    }

    /**
     * 删除会员商品足迹信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return userProdBrowseMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }

}
